package com.ctms.system.controller;

import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.R;
import com.ctms.common.utils.ShiroUtils;
import com.ctms.system.domain.CourseDO;
import com.ctms.system.domain.UserCourseDo;
import com.ctms.system.domain.UserDO;
import com.ctms.system.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/course")
@Controller
public class CourseController {
    @Autowired
    CourseService courseService;

    @GetMapping("")
    public String course(Model model){
        UserDO userDO = ShiroUtils.getUser();
        String username = userDO.getName();
        int identity = userDO.getIdentity();
        model.addAttribute("username",username);
        model.addAttribute("identity",identity);
        return "sys/course/course";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<CourseDO> userCommDos =courseService.selectAllCourse(params);
        if (userCommDos.size()>0){
            for (int i=0;i<userCommDos.size();i++){
                String teacherId=userCommDos.get(i).getCourseTeacher();
                String[] split=teacherId.split(",");
                String teacher="";
                for (int j=0;j<split.length;j++){
                    String teacherName = courseService.getNameById(Integer.parseInt(split[j]));
                    if (j != split.length-1){
                        teacher = teacher + teacherName + ",";
                    }else {
                        teacher = teacher + teacherName;
                    }
                }
                System.out.println(teacher);
                userCommDos.get(i).setCourseTeacher(teacher);
            }
        }
        int total = courseService.count(query);
        PageUtils pageUtil = new PageUtils(userCommDos, total);
        return pageUtil;
    }

    @GetMapping("/add")
    public String add(Model model){
        return "sys/course/add";
    }

    //抽取公共方法,插入中间表
    private String insertUserCourse(CourseDO courseDO){
        //获取课程负责教师的字符串
        String name = courseDO.getCourseTeacher();
        //将字符串进行拆分
        String[] split = name.split(",");
        if (split.length>0){
            for (int i=0;i<split.length;i++){
                UserCourseDo userCourseDo=new UserCourseDo();
                //获取新增的课程编号
                userCourseDo.setCourseNo(courseDO.getCourseNo());
                //获取课程负责教师的用户id
                userCourseDo.setUserId(Integer.parseInt(split[i]));
                //插入课程教师信息表中
                courseService.insertUserCourse(userCourseDo);
            }
        }
        return "success";
    }

    @PostMapping("/save")
    @ResponseBody
    R save(CourseDO courseDO){
        //将前台页面传过来的course信息插入到课程信息表
        if (courseService.insert(courseDO)>0){
            //课程信息插入成功后，将课程信息和课程负责教师插入到课程教师信息表
            insertUserCourse(courseDO);
            //插入成功
            return R.ok();
        }else {
            //插入失败
            return R.error();
        }
    }

    @GetMapping("/edit/{cid}")
    public String edit(Model model,@PathVariable("cid") Integer  cid){
        CourseDO courseDO = courseService.selectById(cid);
        model.addAttribute("course",courseDO);
        return "sys/course/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    R update(CourseDO courseDO){
        if (courseService.update(courseDO)>0){
            courseService.deleteUserCourse(courseDO.getCourseNo());
            insertUserCourse(courseDO);
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/remove")
    @ResponseBody
    R delete(Integer cid){
        if (courseService.deleteCourse(cid)>0){
            return R.ok();
        }else {
            return R.error(-1,"您没有权限删除此课程！");
        }
    }

    @PostMapping("/checkTeacher")
    @ResponseBody
    public String checkTeacherByCid(Integer cid){
        CourseDO courseDO = courseService.selectById(cid);
        String teacherId=courseDO.getCourseTeacher();
        String[] split=teacherId.split(",");
        String teacher="";
        for (int j=0;j<split.length;j++){
            String teacherName = courseService.getNameById(Integer.parseInt(split[j]));
            if (j != split.length-1){
                teacher = teacher + teacherName + ",";
            }else {
                teacher = teacher + teacherName;
            }
        }
        System.out.println(teacher);
        return teacher;
    }

    @PostMapping("/getTeacher")
    @ResponseBody
    public List<UserDO> get(){
        List<UserDO> list = courseService.getTeacher();
        return list;
    }

}
